<?php
/* 
This is library file to payment modules. Cant be reused to 
initalize admin area configuration options.

Copyright (c) 2010 Ingvar Harjaks <ingvarharjaks@gmail.com>
All costs of this development were covered by open source fiendy
company BrightSide OÜ <www.brightside.ee>

Pizza is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Pizza is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Pizza. If not, see <http://www.gnu.org/licenses/>.
*/

class Pizza extends Controller {
  private $error = array(); 
  public $prefix;
  public $def_url;

  /* This will load basic translations for payment module. Payment module
     specific translations will be loaded on its own controller if needed. */
  public function load_def_lang() {
    $this->load->language('common/pizza');
    /* Global stuff, not from module language file. */
    $this->data['heading_title'] = $this->language->get('heading_title');
   $this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_all_zones'] = $this->language->get('text_all_zones');
    $this->data['button_save'] = $this->language->get('button_save');
    $this->data['button_cancel'] = $this->language->get('button_cancel');
    $this->data['tab_general'] = $this->language->get('tab_general');
    /* Fileds. */
    $this->data['entry_username'] = $this->language->get('entry_username');
    $this->data['entry_private_key'] = 
      $this->language->get('entry_private_key');
    $this->data['entry_private_key_pw'] = 
      $this->language->get('entry_private_key_pw');
    $this->data['entry_public_key'] = $this->language->get('entry_public_key');
    $this->data['entry_vk_acc'] = $this->language->get('entry_vk_acc');
    $this->data['entry_vk_name'] = $this->language->get('entry_vk_name');
    $this->data['entry_bank_url'] =  $this->language->get('entry_bank_url');
    $this->data['entry_order_status'] = 
      $this->language->get('entry_order_status');
    $this->data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
    $this->data['entry_status'] = $this->language->get('entry_status');
    $this->data['entry_sort_order'] = 
      $this->language->get('entry_sort_order');
  }

  /* Gen default fields.
     TODO: enable to extend directly from payment module. */
  public function make_def_fields($acc_editable=true) {
    $new_fields = array("username", "key_pub", "key_sec", "key_sec_pw", 
			"vk_acc", "vk_name", "order_status_id",
			"geo_zone_id", "bank_url", "status", "sort_order");
    $this->data["acc_editable"] = $acc_editable;
    $acc_fields = array("vk_acc", "vk_name");
    foreach ($new_fields as $field) {
      if ($acc_editable == false && in_array($field, $acc_fields)) {
	continue;
      }
      $field_name = vsprintf("%s_%s", array($this->prefix, $field));
      if (isset($this->request->post[$field_name])) {
	$this->data[$field_name] = $this->request->post[$field_name];
      } else {
	$this->data[$field_name] = $this->config->get($field_name);
      }
    }
    /* Apply list of different statuses */
    $this->load->model('localisation/order_status');		
    $this->data['order_statuses'] = 
      $this->model_localisation_order_status->getOrderStatuses();
    /* Apply list of different geo zones */
    $this->load->model('localisation/geo_zone');	
    $this->data['geo_zones'] = 
      $this->model_localisation_geo_zone->getGeoZones();
    /* And finally, buttons */
    $this->data['action'] = HTTPS_SERVER . vsprintf('index.php?route=payment/%s&token=%s', 
						    array($this->prefix, $this->session->data['token']));
    $this->data['cancel'] = HTTPS_SERVER . sprintf('index.php?route=extension/payment&token=%s',
						   $this->session->data['token']);
  }
  
  /* Load template file and apply all the *stuff* generated before. */
  public function template_out($acc_editable=true) {
    $this->load->model('setting/setting');
    /* The following block will run if save if requested. */
    if (($this->request->server['REQUEST_METHOD'] == 'POST') 
	&& ($this->validate($acc_editable))) {
      $this->model_setting_setting->editSetting($this->prefix, 
						$this->request->post);
      $this->session->data['success'] = $this->language->get('text_success');

      $this->redirect(HTTPS_SERVER . 'index.php?route=extension/payment&token=' . $this->session->data['token']);
    }

    /* Must pass static data separately */
    $this->data['prefix'] = $this->prefix;
    $this->data['def_bank_url'] = $this->def_url;

    if (count($this->error) != 0) {
      foreach ($this->error as $err_key => $err_value) {
	if (strlen($err_value) != 0) {
	  $this->data[$err_key] = $err_value;
	}
      }
    }

    $this->template = 'common/pizza.tpl';
    $this->children = array("common/header", "common/footer");
    $this->response->setOutput($this->render(TRUE), 
			       $this->config->get('config_compression'));
  }

  /* This function will be called before save to validate permission and
     data of required fields. */
  private function validate($acc_editable=true) {
    $this->load->language('common/pizza');
    if (!$this->user->hasPermission('modify', 'payment/'.$this->prefix)) {
      $this->error['error_warning'] = $this->language->get('error_permission');
    }
    if (!$this->request->post[sprintf('%s_username', $this->prefix)]) {
      $this->error['error_username'] = $this->language->get('error_username');
    } 
    if (!is_file((string)$this->request->
		     post[sprintf('%s_key_pub', $this->prefix)])) {
      $this->error['error_key_pub'] = 
	$this->language->get('error_public_key');
    }
    if (!is_file((string)$this->request->
		     post[sprintf('%s_key_sec', $this->prefix)])) {
      $this->error['error_key_sec'] = 
	$this->language->get('error_private_key');
    }
    if ($acc_editable == true) {
      if (strlen((string)$this->request->
		 post[sprintf('%s_vk_acc', $this->prefix)]) == 0) {
	$this->error['error_vk_acc'] = 
	  $this->language->get('error_vk_acc');
      }
      if (strlen((string)$this->request->
		 post[sprintf('%s_vk_name', $this->prefix)]) == 0) {
	$this->error['error_vk_name'] = 
	  $this->language->get('error_vk_name');
      }
    }
    if (strlen((string)$this->request->
	      post[sprintf('%s_bank_url', $this->prefix)]) == 0) {
	$this->error['error_bank_url'] = 
	  $this->language->get('error_bank_url');
    }

    if (!$this->error) {
      return true;
    } else {
      return false;
    }	
  }
}